class WaveView {
    currentX = 0;
    currentY = 0;
    lastX = 0;
    lastY = 0;
    // 每次画几个点
    step = 8;
    // Y值最大值
    yMax = 100;
    // 每个波形的高度
    itemHeight = 100;
    // 橡皮檫宽度
    clearGap = 16;
    y_offset = 0;
    // 数据队列
    queue = [];
    strokeStyle = "#0f0";
    /**
     * @param frameSize 1秒多少个点
     * @param yMax
     * @param y_offset y偏移
     * @param step 每次画几个点
     * @param speedRatio 扫纸速度，默认 25mm/s (1秒25个小格子 每个小格子0.04s)。 0.5表示扫纸速度为 12.5mm/s。2表示扫纸速度为 50mm/s。
     * @param strokeStyle 线的样式
     */
    constructor(ctx,lineCtx,frameSize, yMax, y_offset, step, speedRatio, strokeStyle,x_start,grid_width,width,hLineNum,vLineNum,txt) {
        this.txt = txt;//左上
        this.ctx = ctx;//画格子
        this.hLineNum = hLineNum;//画格子
        this.vLineNum = vLineNum;//画格子
        this.lineCtx = lineCtx;
        this.x_start = x_start;
        this.grid_width = grid_width;
        this.width = width;
        this.height = yMax;//高度
        this.frameSize = 128;
        this.yMax = yMax;
        this.itemHeight = yMax;
        this.lastY = -1;
		this.isloop=false,//没有在画
        this.y_offset = y_offset;
        // this.step = step;
        this.speedRatio = 1;
        this.strokeStyle = strokeStyle;
        this.drawInterval = Math.floor((1 / this.frameSize) * 1000 * this.step); // 绘制时间间隔
    }

    draw = () => {//画波形
        this.lineCtx.beginPath();
        this.lineCtx.lineWidth = 2;
        this.lineCtx.strokeStyle = this.strokeStyle;
        for (let i = 0; i < this.step; i++) {
			if (this.lastX === 0) {
			    this.lineCtx.clearRect(this.x_start - 2, this.y_offset, this.clearGap, this.itemHeight);
			} else {
			    this.lineCtx.clearRect(this.x_start + this.lastX, this.y_offset, this.clearGap, this.itemHeight);
			}
            if (this.queue.length === 0) {
                // this.currentY = this.itemHeight / 2;
                return false
            } else {
                this.currentY = (-1.0 * this.queue.shift()) / this.yMax * this.itemHeight + this.itemHeight;
            }
            if (this.currentY > this.itemHeight) {
                this.currentY = this.itemHeight;
            }
            if (this.lastY === -1)
                this.lastY = this.currentY
            this.lineCtx.moveTo(this.x_start + this.lastX, this.y_offset + this.lastY);
            if (this.currentY > -1000 ){
                this.lineCtx.lineTo(this.x_start + this.currentX, this.y_offset + this.currentY);
                this.lastY = this.currentY;
            }
            this.lastX = this.currentX;
            this.currentX += (this.grid_width * 25 * this.speedRatio) / this.frameSize;
            if (this.x_start + this.currentX >= this.width) {
                this.currentX = 0;
                this.lastX = 0;
            }
        }
        this.lineCtx.stroke();
		this.lineCtx.draw(true);
    }

    loop = () => {
        this.draw();
		// console.log(Math.floor((1 / this.frameSize) * 1000 * this.step));
		// setTimeout(this.loop,Math.floor((1 / this.frameSize) * 1000 * this.step);
		setTimeout(this.loop, this.drawInterval);
		// console.log(this.drawInterval);
        // TODO for test only 添加测试数据，实际中会从后台取
    }

    addData = (arr,index) => {//心电
		// console.log(arr);
        this.queue.push(arr[index]*this.height/3+this.height/2);
		
    }
    addDataSPO = (arr) => {//血氧
        // arr = this.putSingleV8EcgDataBuffer(this.btye(this.arrSlice(this.base64ToUint8Array(arr))))
        //canvas高度300为基准
        //一大格为0.5毫伏  this.grid_width = height / 30有3毫伏
        //this.height/3 画出来有6格 6*0.5=3  this.height/2中间开始画所以+
		
		// console.log(this.queue);
        for (let i = 0; i < arr.length; i++) {
            // this.queue.push(arr[0]*250+height/2);
            this.queue.push(arr[i]);
        }
		// this.draw();
    }

    arrSlice(data){//每三个字节对应一个通道AD值
        let arr2=[]
        for (let i = 0; i < data.length; i += 3) {
            arr2.push(data.slice(i, i + 3))
        }
        return arr2
    }
    btye(data){//每三个字节对应一个通道AD值
        let arr=[]
        data.map(decodedData => {
            for (let i = 0; i < decodedData.length; ) {
                let value = 0;
                let b = decodedData[i++];
                value |= (b & 0x000000ff);
                value <<= 8;
                b = decodedData[i++];
                value |= (b & 0x000000ff);
                value <<= 8;
                b =  decodedData[i++];
                value |= (b & 0x000000ff);
                arr.push(value)
            }
        })
        return arr
    }
    ad2mv( value, original) {//换算毫伏值
        if (original)
            return (value - 0x800000) * 0.476837 / 12 / 1000;
        return (value - 6000) / 820.0;
    }
    mvData(data8Channel){//换算12导联
        let mvData = [];
        let original= true;
        mvData[0] = (this.ad2mv(data8Channel[0], original));
        mvData[1] = (this.ad2mv(data8Channel[1], original));
        mvData[2] = (this.ad2mv(data8Channel[1], original) - this.ad2mv(data8Channel[0], original));
        mvData[3] = ( -0.5 * (this.ad2mv(data8Channel[1], original) + this.ad2mv(data8Channel[0], original)));
        mvData[4] = (this.ad2mv(data8Channel[0], original) -  0.5 * this.ad2mv(data8Channel[1], original));
        mvData[5] = (this.ad2mv(data8Channel[1], original) -  0.5 * this.ad2mv(data8Channel[0], original));
        mvData[6] = (this.ad2mv(data8Channel[2], original));
        mvData[7] = (this.ad2mv(data8Channel[3], original));
        mvData[8] = (this.ad2mv(data8Channel[4], original));
        mvData[9] = (this.ad2mv(data8Channel[5], original));
        mvData[10] = (this.ad2mv(data8Channel[6], original));
        mvData[11] = (this.ad2mv(data8Channel[7], original));
        return mvData;
    }
    getMvData(data,timeOut,index){//8个通道对应12导联毫伏值 timeOut差值大于1插入填补数据
        data=this.btye(this.arrSlice(this.base64ToUint8Array(data)))
        // console.log(data)
        if (timeOut > 1500 && timeOut<3000){
            for (let i = 0; i <Math.floor((timeOut-250) / 250)*1000; i+= 8) {
                // ecgWave.addData(mvData(data.slice(i, i + 8)))
                this.addData([9999,9999,9999,9999,9999,9999,9999,9999,9999,9999,9999,9999],index)//12个空
            }
        }
        for (let i = 0; i < data.length; i += 8) {
            if (data.slice(i, i + 8) != undefined){
                this.addData(this.mvData(data.slice(i, i + 8)),index)
            }
        }
        this.addFillText(index);
    }
    base64ToUint8Array(base64String) {//base64转字符串
        if (base64String === undefined){
            return  false;
        }
        const padding = '='.repeat((4 - base64String.length % 4) % 4);
        const base64 = (base64String + padding)
            .replace(/\-/g, '+')
            .replace(/_/g, '/');
        // console.log(base64)
		// console.log(base64String);
        const rawData = uni.base64ToArrayBuffer(base64String)
		// const rawData = atob(base64String)
		// console.log(rawData);
        const outputArray = new Uint8Array(rawData);
		// console.log(outputArray);
        // for (let i = 0; i < rawData.length; ++i) {
        //     outputArray[i] = rawData.charCodeAt(i);
        // }
		// console.log(outputArray);
        return outputArray;
    }


    //画格子
    drawGrid() {
        let {hLineNum,ctx,grid_width,width,vLineNum,height}=this;
        ctx.beginPath();
        ctx.lineWidth = 1;
        ctx.strokeStyle = "#d0d5f3";
        for (let i = 0; i < hLineNum; i=i+5) {
            if (i == 0 || i % 5 != 0) {
                ctx.moveTo(0, i * grid_width);
                ctx.lineTo(width, i * grid_width);
                ctx.stroke();
            }
        }
        ctx.beginPath();
        ctx.lineWidth = 1;
        ctx.strokeStyle = "#d0d5f3";
        for (let i = 0; i < vLineNum; i=i+5) {
            if (i == 0 || i % 5 != 0) {
                ctx.moveTo(i * grid_width, 0);
                ctx.lineTo(i * grid_width, height);
                ctx.stroke();
            }
        }
        ctx.beginPath();
        ctx.lineWidth = 1;
        ctx.strokeStyle = "#d0d5f3";
        for (let i = 5; i <= vLineNum; i += 5) {
            ctx.moveTo(i * grid_width, 0);
            ctx.lineTo(i * grid_width, height);
            ctx.stroke();
        }

        ctx.beginPath();
        ctx.lineWidth = 1;
        ctx.strokeStyle = "#d0d5f3";
        for (let i = 5; i <= hLineNum; i += 5) {
            ctx.moveTo(0, i * grid_width);
            ctx.lineTo(width, i * grid_width);
            ctx.stroke();
        }
        this.drawMark();
        this.addFillText();
		ctx.draw();
    }

    drawMark() {
        let {ctx,grid_width,height}=this
        ctx.beginPath();
        ctx.lineWidth = 2;
        ctx.strokeStyle = "#fe7200";
        ctx.moveTo(0, height / 2 + grid_width * 5);
        ctx.lineTo(grid_width * 3, height / 2 + grid_width * 5);
        ctx.lineTo(grid_width * 3, height / 2 - grid_width * 5);
        ctx.lineTo(grid_width * 7, height / 2 - grid_width * 5);
        ctx.lineTo(grid_width * 7, height / 2 + grid_width * 5);
        ctx.lineTo(grid_width * 10, height / 2 + grid_width * 5);
        ctx.stroke();
    }

    addFillText(index) {
        let text;
        text=this.txt
		console.log(text);
        this.ctx.font = "20px Times New Roman";
        this.ctx.fillStyle = "#000";
        this.ctx.fillText(text, 20, 30)
    }

    putSingleV8EcgDataBuffer(buffer) {//血氧
        let bufferList=[]
        for (let i = 0; i < buffer.length; i ++) {
            bufferList.push(this.ad2mv(buffer[i], true));
        }
        return bufferList
    }
}

export default WaveView;
